<?php
include '../config.php';
include 'utils.php';

session_start();

$location = "Location: " . $ISSUER_HOST . "/create_category_result.php";

// Connecting, selecting database
$mysqli = new mysqli($MYSQL_HOST, $MYSQL_USERNAME, $MYSQL_PASSWORD, $MYSQL_DB_NAME);
if ($mysqli->connect_errno) {
	$_SESSION[$CB_MSG_TAG] = $DB_CONN_ERR;
	$_SESSION[$DB_CONN_ERRNO] = $mysqli->connect_errno;
	$_SESSION[$DB_CONN_ERROR] = $mysqli->connect_error;
	header($location);
	exit();
}
$name = prepareString($mysqli, $_POST['nome']);
$catPadre = prepareString($mysqli, $_POST['categoriaPadre']);

// Performing SQL query
$query = sprintf($INSERT_CATEGORY, $name, $catPadre);

$result = $mysqli->query($query);

if (!$result) {
	mysqli_free_result($result);
	$mysqli->close();
	$_SESSION[$CB_MSG_TAG] = $QUERY_ERR;
	$_SESSION[$QUERY_TAG] = $query;
	$_SESSION[$QUERY_ERRNO] = $mysqli->errno;
	$_SESSION[$QUERY_ERROR] = $mysqli->error;
	header($location);
	exit();
}

mysqli_free_result($result);

// Closing connection
$mysqli->close();

$_SESSION[$CB_MSG_TAG] = $MSG_OK;
header($location);
exit();
?>
